public class SinlyLink<T>
{
    public Node<T> head;

    public SinlyLink()
    {
        this.head = new Node<T>();
    }

    public boolean isEmpty()//判断是否空
    {
        return this.head == null;
    }

    public int size()//返回元素个数（长度）
    {
        int count = 0;
        Node<T> p = this.head.next;
        while (p.next != null)
        {
            p = p.next;
            count++;
        }
        return count;
    }

    public String toString()//所有元素的描述字符串
    {
        String str = this.getClass().getName() + "(";
        for (Node<T> p = this.head.next; p != null; p = p.next)
        {
            str += p.data.toString() + (p.next != null ? "," : "");
        }
        return str + ")";
    }

    public int search2(T key)//查找并返回与key相等元素对应索引
    {
        Node<T> p = this.head.next;
        for (int i = 0; i < size() - 1; i++)
        {
            if (p.data == key)
            {
                return i;
            }
            p = p.next;
        }
        return -1;
    }

    public Node<T> search(T key)//查找并返回与key相等元素
    {
        Node<T> p = this.head.next;
        for (int i = 0; i < size() - 1; i++)
        {
            if (p.data == key)
            {
                return p;
            }
            p = p.next;
        }
        return null;
    }

    public T remove(T key)//查找并删除与key相等元素
    {
        Node<T> p = this.head.next;
        for (int i = 0; i < size() - 1; i++)
        {
            if (p.data == key)
            {
                return p.data;
            }
            p = p.next;
        }
        return null;
    }

    public T remove(int i)//查找并删除第i个元素
    {
        Node<T> p = this.head;
        for (int j = 0; j < i && p.next != null; j++)
        {
            p=p.next;
        }

        if (i>=0 && p.next != null)
        {
            p.next=p.next.next;
        }
        return null;
    }


    public void clear()//删除所有元素
    {
        this.head.next = null;
    }

    public boolean equals(Object obj)//比较this与obj是否相等
    {
        Node<T> p = this.head.next;
        for (int i = 0; i <= size(); i++)
        {
            if (p.data==obj)
            {
                return true;
            }
            p=p.next;
        }
        return false;
    }

    //线性表增加以下方法，参数i表示元素序号，指定操作位置

    public T get(int i)//返回第i个元素数据域的数据
    {
        Node<T> p = this.head.next;
        for (int j = 0; j < i && p.next != null; j++)
        {
            p=p.next;
        }
        return (i>=0 && p!=null) ? p.data : null;
    }
    public Node<T> getNode(int i)//返回第i个结点对应元素
    {
        Node<T> p = this.head.next;
        for (int j = 0; j < i && p.next != null; j++)
        {
            p=p.next;
        }
        return (i>=0 && p!=null) ? p : null;
    }

    public void set(int i, T x)//设置第i个元素为x
    {
        Node<T> p = this.head.next;
        for (int j = 0; j < i && p.next != null; j++)
        {
            p=p.next;
        }
        p.data = x;
    }
    public int insert(int i, T x)//插入x作为第i个元素
    {
        Node<T> p = this.head;
        for (int j = 0; j < i && p.next != null; j++)
        {
            p=p.next;
        }
        p.next = new Node<T>(p.next,x);
        return (int) p.next.data;
    }


    public int insert(T x)//在最后插入x元素
    {
        Node<T> p = this.head;
        while(p.next != null)
        {
            p=p.next;
        }
        p.next = new Node<T>(null,x);
        return 0;
    }
}